//
//  ZMReweetView.swift
//  ZMWeiboSwift
//
//  Created by pg on 2017/7/26.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit
import SnapKit
//转发微博view
class ZMReweetView: UIView {

    //记录转发微博view底部约束
   var  retweetViewBottomContriants : Constraint?
    
    
    //引用viewModel
    var statusViewModel: ZMStatusViewModel? {
        didSet{
            contentsLabel.attributedText  = statusViewModel?.retweedStatusesAttributeStr
            
            //判断.当配图数量大于0 的时候才加载配图视图,
            //移除转发视图底部约束
            retweetViewBottomContriants?.deactivate()
            
            if let picUrls = statusViewModel?.statusesModel?.retweeted_status?.pic_urls , picUrls.count > 0 {
                
                //有配图
                
                pictureView.isHidden = false
                
                self.snp.makeConstraints({ (make) in
                    retweetViewBottomContriants = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
                //显示配图view的数据源赋值
                pictureView.picUrlArray = picUrls
                
                
                
                
            }else
            {
                //没有配图,这个时候隐藏配图view
                pictureView.isHidden = true
                //更新配图view的大小约束
                self.snp.makeConstraints({ (make) in
                    retweetViewBottomContriants = make.bottom.equalTo(contentsLabel).offset(HomeCellMargin).constraint
                })
                
            }
            
            
            
            
        }
    }
    
    //懒加载控件,,显示转发微博内容的label
    private lazy var contentsLabel : UILabel = {
       
        let label = UILabel(text: "", fontSize: 13, textColor: UIColor.lightGray)
        label.numberOfLines = 0
        return label
        
    }()
    //显示配图的view
    fileprivate lazy var pictureView : ZMHomeCellPictureView = {
        
        let picview = ZMHomeCellPictureView()
        picview.backgroundColor = UIColor.white
        return picview
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI() -> Void {
        //加载label,设置约束
        addSubview(contentsLabel)
        contentsLabel.snp.makeConstraints { (make) in
            make.top.equalTo(self).offset(HomeCellMargin)
            make.leading.equalTo(self).offset(HomeCellMargin)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
           // make.bottom.equalTo(self).offset(-HomeCellMargin)
        }
        //添加配图view
        addSubview(pictureView)
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(contentsLabel.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(contentsLabel)
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
            
         //   make.bottom.equalTo(self).offset(-HomeCellMargin)
        }
        
        //设置当前根视图底部约束,用于自动计算行高
        self.snp.makeConstraints { (make) in
           retweetViewBottomContriants = make.bottom.equalTo(pictureView.snp.bottom).offset(HomeCellMargin).constraint
        }
        
    }
    
     
    
}
